package com.canaan.business.controller.pc.basic;

import java.util.List;

import com.canaan.common.config.RuoYiConfig;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.file.FileUploadUtils;
import com.canaan.common.utils.file.MimeTypeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import com.canaan.business.domain.basic.StorehousePlat;
import com.canaan.business.service.basic.IStorehousePlatService;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 仓库绘制地图信息Controller
 *
 * @author 
 * @date 2023-08-04
 */
@Api(tags = "仓库绘制地图信息")
@RestController
@RequestMapping("/basic/storehousePlat")
public class StorehousePlatController extends BaseController {
    @Resource
    private IStorehousePlatService storehousePlatService;

    /**
     * 查询仓库绘制地图信息列表
     */
    @ApiOperation(value = "查询仓库绘制地图信息列表")
    @GetMapping("/list")
    public TableDataInfo list(StorehousePlat storehousePlat) {
        startPage();
        List<StorehousePlat> list = storehousePlatService.selectStorehousePlatList(storehousePlat);
        return getDataTable(list);
    }

    /**
     * 导出仓库绘制地图信息列表
     */
    @ApiOperation(value = "导出仓库绘制地图信息列表")
    @Log(title = "仓库绘制地图信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StorehousePlat storehousePlat) {
        List<StorehousePlat> list = storehousePlatService.selectStorehousePlatList(storehousePlat);
        ExcelUtil<StorehousePlat> util = new ExcelUtil<StorehousePlat>(StorehousePlat.class);
        util.exportExcel(response, list, "仓库绘制地图信息数据");
    }

    /**
     * 获取仓库绘制地图信息详细信息
     */
    @ApiOperation(value = "获取仓库绘制地图信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(storehousePlatService.selectStorehousePlatById(id));
    }
    @ApiOperation(value = "获取库位绘制地图信息详细信息")
    @GetMapping(value = "/getInfoByStorehouseNo/{storehouseNo}")
    public AjaxResult getInfoByStorehouseNo(@PathVariable("storehouseNo")  String storehouseNo){
        return success(storehousePlatService.getInfoByStorehouseNo(storehouseNo));
    }

    /**
     * 新增仓库绘制地图信息
     */
    @ApiOperation(value = "新增仓库绘制地图信息")

    @Log(title = "仓库绘制地图信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StorehousePlat storehousePlat) {
        return toAjax(storehousePlatService.insertStorehousePlat(storehousePlat));
    }

    /**
     * 修改仓库绘制地图信息
     */
    @ApiOperation(value = "修改仓库绘制地图信息")

    @Log(title = "仓库绘制地图信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StorehousePlat storehousePlat) {
        return toAjax(storehousePlatService.updateStorehousePlat(storehousePlat));
    }

    /**
     * 删除仓库绘制地图信息
     */
    @ApiOperation(value = "删除仓库绘制地图信息")

    @Log(title = "仓库绘制地图信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(storehousePlatService.deleteStorehousePlatByIds(ids));
    }
    @ApiOperation(value = "以树形查询仓库所有信息")
    @GetMapping("/selectStoreHouseByTree/{storeHouseNo}")
    public AjaxResult selectStoreHouseByTree(@PathVariable("storeHouseNo") String storeHouseNo){
        return  success(storehousePlatService.selectStoreHouseAllTree(storeHouseNo));
    }

    /**
     * 仓库图片上传
     */
    @Log(title = "仓库图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadStoreHouse")
    public AjaxResult uploadStoreHouse(@RequestParam("storeHousefile") MultipartFile file,@RequestParam("storeHouseNo") String storeHouseNo) throws Exception
    {
        if (!file.isEmpty())
        {
            String url = FileUploadUtils.upload(RuoYiConfig.getStoreHousePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (StringUtils.isNotEmpty(storeHouseNo)){
                storehousePlatService.uploadStoreHouse(storeHouseNo,url);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
}
